package cn.kk.jump;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class World extends JPanel{
    public static final int WIDTH=576;/**窗口的宽*/
    public static final int HEIGHT=363;/**窗口的高*/

    public static final int START=0;//游戏启动
    public static final int RUNNING=1;//运行
    public static final int PAUSE=2;//暂停
    public static final int GAME_OVER=3;//结束
    private int state=START;//当前状态(默认状态)

    private Sky sky = new Sky();//创建背景天空对象
    private Gamer gamer = new Gamer();//创建玩家对象
    private Box[] boxes = {};//创建箱子


    /**越界删除*/
    public void outOfBoundsAction() {//每10ms走一次
        for (int i = 0; i < boxes.length; i++) {//遍历所有敌人
            if (boxes[i].isOutOfBounds() || boxes[i].isRemove()) {//越界了
                boxes = Arrays.copyOf(boxes, boxes.length - 1);//缩容
            }
        }
    }
    /**检测游戏结束*/
    public void checkGameOverAction(){
        if (gamer.getLife()<=0){
            state=GAME_OVER;
        }
    }
    /**移动*/
    public void stepAction(){//每10ms走一次
        sky.step();
        for (int i = 0; i < boxes.length; i++) {
            boxes[i].step();
        }
        for (int j = 0; j < boxes.length; j++) {
            boxes[j].yStep();
        }
    }

    /**生成箱子对象*/
    public Box nextOne(){
        return new Box();
    }

    /**箱子入场*/
    private int enterIndex=0;
    public void enterAction(){
        enterIndex++;//每10ms增加1
        if (enterIndex%40==0){//每40ms走一次
            Box bb = nextOne();
            boxes = Arrays.copyOf(boxes,boxes.length+1);
            boxes[boxes.length-1]=bb;
        }
    }
    /**程序启动*/
    public void action(){
        MouseAdapter m = new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                switch (state){
                    case START:
                        state=RUNNING;
                        break;
                    case GAME_OVER:
                        sky=new Sky();
                        gamer=new Gamer();
                        boxes = new Box[0];
                        state=START;
                        break;
                    case RUNNING:
                        gamer.jump();
                        break;
                }
            }
        };//鼠标侦听器
        this.addMouseListener(m);
        this.addMouseMotionListener(m);
        Timer timer = new Timer();
        int intervel =10;
        timer.schedule(new TimerTask() {
            public void run() {//定时
                if (state==RUNNING){
                    enterAction();//箱子入场
                    stepAction();
                    outOfBoundsAction();//越界删除
                }
                repaint();
            }
        },intervel,intervel);
    }



    /**画窗口*/
    public void paint(Graphics g){
        g.drawImage(sky.getImage(),sky.x,sky.y,null);
        g.drawImage(sky.getImage(),sky.x,sky.getY1(),null);
        g.drawImage(gamer.getImage(), gamer.x, gamer.y, null);
        for (int i = 0; i < boxes.length; i++) {
            Box c = boxes[i];
            g.drawImage(c.getImage(),c.x,c.y,null);
        }
    }


    //画窗口区域
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World world = new World();
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH,HEIGHT);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);//尽快调用paint()方法

        world.action();//启动程序的执行
    }
}
